package com.haitai.haitaitv.component.redis.serialzer;

/**
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.nustaq.serialization.FSTObjectInput;
import org.nustaq.serialization.FSTObjectOutput;
import com.jfinal.kit.LogKit;
import redis.clients.util.SafeEncoder;

/**
 * FstSerializer.
 */
public class FstSerializer implements ISerializer {
    
    public static final ISerializer me = new FstSerializer();
    
    public byte[] keyToBytes(String key) {
        return SafeEncoder.encode(key);
    }
    
    public String keyFromBytes(byte[] bytes) {
        return SafeEncoder.encode(bytes);
    }
    
    public byte[] fieldToBytes(Object field) {
        return valueToBytes(field);
    }
    
    public Object fieldFromBytes(byte[] bytes) {
        return valueFromBytes(bytes);
    }
    
    public byte[] valueToBytes(Object value) {
        FSTObjectOutput fstOut = null;
        try {
            ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
            fstOut = new FSTObjectOutput(bytesOut);
            fstOut.writeObject(value);
            fstOut.flush();
            return bytesOut.toByteArray();
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
        finally {
            if(fstOut != null)
                try {fstOut.close();} catch (IOException e) {LogKit.error(e.getMessage(), e);}
        }
    }
    
    public Object valueFromBytes(byte[] bytes) {
        if(bytes == null || bytes.length == 0)
            return null;
        
        FSTObjectInput fstInput = null;
        try {
            fstInput = new FSTObjectInput(new ByteArrayInputStream(bytes));
            return fstInput.readObject();
        }
        catch (Exception e) {
            throw new RuntimeException(e);
        }
        finally {
            if(fstInput != null)
                try {fstInput.close();} catch (IOException e) {LogKit.error(e.getMessage(), e);}
        }
    }
}




